import numpy as np
import csv
import random
from datetime import datetime
from time import strftime
from dbfpy import dbf
from scipy.stats import scoreatpercentile

'''
revision of test24
to calculate the false nagetive, false positive

'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def build_satscanresult_dbf(inputDBF):
    sKey = np.array([])
    fn = inputDBF
    db = dbf.Dbf(fn)
    for record in db:
        temp = float(record[db.fieldNames[2]])
        temp_id = int(float(record[db.fieldNames[0]]))
        sKey = np.append(sKey, temp_id)
        temp_id = int(float(record[db.fieldNames[1]]))
        sKey = np.append(sKey, temp_id)
        sKey = np.append(sKey, temp)
    sKey.shape = (-1, 3)
    return sKey

def build_satscanresult_pvalue(arrayLen, inputDBF):
    # build a list of pvalue, if not found in DBF, set as 1
    tPvalue = np.ones(arrayLen)
    tempResult = build_satscanresult_dbf(inputDBF)
    #i = 0
    for item in tempResult:
        tPvalue[int(item[0])] = item[2]
    return tPvalue

def build_region_list(inputCSV):
    temp =  build_data_list(inputCSV)
    temp_list = temp[np.argsort(temp[:,0]),:]
    #temp_idset = set(temp_list[:,1])
    temp_idDict = {}
    i = 0
    for item in temp_list:
        if str(int(item[1])) not in temp_idDict.keys():
            temp_idDict[str(int(item[1]))] = i
            i += 1
    #print i
    temp_idlist = []
    for item in temp_list[:,1]:
        temp_idlist.append(temp_idDict[str(int(item))])
    return temp_idlist

def cal_region_attri(region_id):
    dis_reg = np.unique(region_id)
    iLen = dis_reg.shape[0]
    temp_reg_attri = np.zeros((iLen, 3)) #[caner, pop, rate]

    # unit_attri = [cancer, pop, area, rate]

    i = 0
    for item in unit_attri:
        temp_reg_attri[int(region_id[i]),0] += item[0]
        temp_reg_attri[int(region_id[i]),1] += item[1]
        #temp_reg_attri[int(region_id[i]),2] + = item[2]
        i = i + 1

    for item in temp_reg_attri:
        item[2] = item[0]/item[1]

    return temp_reg_attri[:,2]

def cal_unit_rate(inputCSV):           
    region_id = build_region_list(inputCSV)
    reg_rate = cal_region_attri(region_id) # [rate]
    rate = []
    for item in region_id:
        rate.append(reg_rate[int(item)])
    return rate, region_id

def build_unit_pvalue(inputCSV):
    unit_pvalue = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print float(record[ra.fieldnames[-1]]), type(record[ra.fieldnames[-1]])
        unit_pvalue.append(float(record[ra.fieldnames[pvalueLevel]]))
    return unit_pvalue

def cal_quantile(inputdata, q):
    temp = []
    #print len(inputdata[0])
    for i in range(0, len(inputdata[0])):
        #print scoreatpercentile(inputdata[:,0], 25)
        temp.append(scoreatpercentile(inputdata[:,i], int(q), limit = ()))
    return temp

def addQuatile(tempList):
    iLen = len(tempList)
    temp_mean = tempList.mean(axis=0)
    temp_1Q = cal_quantile(tempList, 25)
    temp_median = np.median(tempList, axis=0)
    temp_3Q = cal_quantile(tempList, 75)
    #temp_std = case_measure.std(axis=0)
            
    tempList = np.append(tempList, temp_mean)
    tempList = np.append(tempList, temp_1Q)
    tempList = np.append(tempList, temp_median)
    tempList = np.append(tempList, temp_3Q)
    #case_measure = np.append(case_measure, temp_std)
    tempList.shape = (iLen + 4, -1)
    return tempList



#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()

    
    # old risk area

    mixed = [91,98,101,104,114,115,119,126,131,142,146,147,154,162,168,172]
    rural = [8,9,10,11,12,13,14,15,17,19,20,26,28,33,34,37]
    urban = [105,107,112,120,122,125,127,128,130,133,134,141,143,149,152,155]

    hot_1 = [9,130,147]
    hot_2 = hot_1 + [10,133,154]
    hot_4 = hot_2 + [12,17,125,131,141,146]
    hot_8 = hot_4 + [14,19,20,26,114,115,119,120,128,134,149,168]
    hot_16 = mixed + rural + urban
    
    hot_16 = [9, 10, 12, 13, 14, 17, 20, 23, 26, 27, 28, 33, 34, 35, 38, 41,
              95, 99, 109, 110, 114, 115, 117, 119, 125, 126, 127, 128, 130,
              131, 133, 134, 136, 137, 138, 139, 140, 141, 142, 143, 146, 147,
              149, 151, 152, 156, 157, 237 ]
    '''
    # new 2 risk area
    mixed = [95,99,109,110,114,115,117,119,126,131,139,140,142,146,147,237] # pop: 1611198
    rural = [9,10,12,13,14,17,20,23,26,27,28,33,34,35,38,41]    # pop: 501040  
    urban = [125,127,128,130,133,134,136,138,141,143,149,151,152,156,157,163] # pop: 7025156
    '''
    hot_16 = mixed + rural + urban
    
    unitCSV = 'C:/_DATA/CancerData/SatScan/mult6000/three16_format.csv'
    #unitCSV = 'C:/_DATA/CancerData/SatScan/own/temp_6000.csv'
    dataMatrix = build_data_list(unitCSV)  # [id, pop, cancer1, cancer2, cancer3]

    pop = [0, 0, 0, 0]  # outside, mixed, rural, urban
    
    for id in dataMatrix[:,0]:
        if int(id) in mixed:
            pop[1] += dataMatrix[int(id), 1]
        elif int(id) in rural:
            pop[2] += dataMatrix[int(id), 1]
        elif int(id) in urban:
            pop[3] += dataMatrix[int(id), 1]
        else:
            pop[0] += dataMatrix[int(id), 1]
        
    #filePath = 'C:/_DATA/CancerData/SatScan/own/modified/6000/satscan/'
    filePath = 'C:/_DATA/CancerData/SatScan/own/'

    repeatTime = 1000   
    row = 0
    count = 0

    rate = []

    for repeat in range(0, repeatTime):
        row += 1
        if int(row/100) > 0:
            #print count*100
            row = 0
            count += 1
        #unit_attri[:,0] = dataMatrix[:,repeat + 2] # [cancer, pop, pvalue]
        case = [0, 0, 0, 0]
        for id in dataMatrix[:,0]:
            if int(id) in mixed:
                case[1] += dataMatrix[int(id), repeat + 2]
            elif int(id) in rural:
                case[2] += dataMatrix[int(id), repeat + 2]
            elif int(id) in urban:
                case[3] += dataMatrix[int(id), repeat + 2]
            else:
                case[0] += dataMatrix[int(id), repeat + 2]
        for i in range(0,4):
            rate.append((case[i]+0.0)/pop[i])

    rate = np.array(rate)
    rate.shape = (repeatTime, -1)
    rate = addQuatile(rate)

    print unitCSV
    print rate[1000,:]
    print np.var(rate[:1000,0]), np.var(rate[:1000,1]), np.var(rate[:1000,2]), np.var(rate[:1000,3])
      
    fileLoc = unitCSV[:-4] + '_rate.csv'
    np.savetxt(fileLoc, rate, delimiter=',', fmt = '%10.10f')


    print "end at " + getCurTime()
    print "========================================================================"  

           
